<%@ page import="org.f0rb.demo.common.Message" %>
<%@ page import="org.f0rb.demo.model.user.User" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page pageEncoding="UTF-8" %>
<%
    User userDTO = (User) request.getAttribute("model");
%>
<!DOCTYPE HTML>
<html lang=<%=Message.locale()%>>
<head>
<jsp:include page="/WEB-INF/jsp/head/jquery.jsp"/>
</head>
<body>
<table>
    <thead>
    <tr>
        <th>编号</th>
        <th>用户ID</th>
        <th>用户名</th>
        <%--<% for (Role role : Configuration.ROLES) { %>
        <th><span><a href="/role/view?id=<%=role.getId()%>"><%=role.getName()%>
        </a></span></th>
        <% } %>--%>
        <th>注册时间</th>
        <th>最近登录时间</th>
    </tr>
    </thead>
    <tfoot>
        <tr><th colspan="3">
            <jsp:include page="/WEB-INF/jsp/common/pagebar.jsp"/>
        </th></tr>
    </tfoot>
    <tbody>
    <%
        for (int i = 0, len = userDTO.getList().size(); i < len; i++) {
            User user = userDTO.getList().get(i);
    %>
    <tr>
        <td><%=i + 1%></td>
        <td><%=user.getId()%></td>
        <td><a href="edit?username=<%=user.getUsername()%>"><%=user.getUsername()%></a></td>
        <%--<% for (Role role : Configuration.ROLES) {%>
        <td><span><input class="xxx" type="checkbox" value="<%=user.getId()%>-<%=role.getId()%>"
                <%=user.hasRole(role) ? "" : "checked"%>/></span></td>
        <% } %>--%>
        <td><%=user.getRegtime()%></td>
        <td><%=user.getLastlogin()%></td>
    </tr>
    <% } %>
    </tbody>
</table>
<script type="text/javascript">
    $(function() {
        $(".xxx").click(function() {
            if (!(this instanceof HTMLInputElement) || this.type != "checkbox") return;
            var elem = this;
            var action = elem.checked ? "/userrole/insert" : "/userrole/delete";
            var arr = elem.value.split('-');
            var param = {
                userid: arr[0],
                roleid: arr[1]
            };
            $.post(action, param, function(data) {
                if (data.messages) {
                    alert(data.messages[action]);
                    elem.checked = !elem.checked;
                }
            });
        })
    })
</script>
</body>
</html>
